<?php


namespace app\common\helper;

use app\common\model\AuthRule;

class MenuHelper
{
    // 前台栏目 导航
    public static function MakeChannel($channel, $pid = 0)
    {
        $list = array();
        $pid = intval($pid);
        foreach ($channel as $k => $v) {
            if ($v['is_part'] == 1) {
                $v['href'] = $v['url'];
            } else {
                $v['href'] = url($v['ca'], ['tid' => $v['id']]);
            }
            if ($v['pid'] == $pid) {
                $v['child'] = self::MakeChannel($channel, $v['id']);
                $list[] = $v;
            }
        }
        return $list;
    }

    // 前台产品分类 导航
    public static function MakeCategory($channel, $pid = 0)
    {
        $list = array();
        $pid = intval($pid);
        foreach ($channel as $k => $v) {
            $v['href'] = url('Goods/index', ['tid' => $v['id']]);
            if ($v['pid'] == $pid) {
                $v['child'] = self::MakeCategory($channel, $v['id']);
                $list[] = $v;
            }
        }
        return $list;
    }

    // 管理后台 左侧菜单树 根据权限设置 后台
    public static function MakeMenu($menu, $pid = 0)
    {
        $adminId = session('admin.id');
        $adminRules = explode(',', session('admin.rules'));

        // 将不需要验证权限的菜单 赋值给用户 去重
        $menus = AuthRule::where(['status' => 0])->order('sort desc')->column('id');
        $adminRules = array_merge($adminRules, $menus);
        $adminRules = array_unique($adminRules);

        $list = array();
        foreach ($menu as $k => $v) {
            $v['url'] = strtolower(url($v['url']));
            if ($adminId != 1) {
                if ($v['pid'] == $pid) {
                    if (in_array($v['id'], $adminRules)) {
                        $v['child'] = self::MakeMenu($menu, $v['id']);
                        $list[] = $v;
                    }
                }
            } else {
                if ($v['pid'] == $pid) {
                    $v['child'] = self::MakeMenu($menu, $v['id']);
                    $list[] = $v;
                }
            }
        }
        return $list;
    }

    // 用户后台 左侧菜单树 根据权限设置
    public static function MakeUserMenu($menu, $pid = 0)
    {
        $userId = session('user.id');
        $userRules = explode(',', session('user.rules'));

        // 将不需要验证权限的菜单 赋值给用户 去重
//        $menus = AuthRule::where(['status' => 0])->order('sort desc')->column('id');
//        $adminRules = array_merge($adminRules, $menus);
//        $adminRules = array_unique($adminRules);

        $list = array();
        foreach ($menu as $k => $v) {
            $v['url'] = strtolower(url($v['url']));
            if ($v['pid'] == $pid) {
                $v['child'] = self::MakeUserMenu($menu, $v['id']);
                $list[] = $v;
            }
        }
        return $list;
    }
    public static function MakeAdminMenuTable($menu, $pid = 0)
    {
        $list = array();
        foreach ($menu as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['children'] = self::MakeAdminMenuTable($menu, $v['id']);
                $list[] = $v;
            }
        }
        return $list;
    }
    // 返回 element tree
    public static function makeElementTree($menu, $pid = 0)
    {
        $list = array();
        foreach ($menu as $k => $v) {
            $temp = [];
            if ($v['pid'] == $pid) {
                $temp["id"] = $v["id"];
                $temp["label"] = $v["title"];
                $temp['children'] = self::makeElementTree($menu, $v['id']);
                $list[] = $temp;
            }
        }
        return $list;
    }


    /**
     * 自定义菜单排列  后台
     */
    public static function menu($cate, $lefthtml = '|— ', $pid = 0, $lvl = 0, $leftpin = 0)
    {
        $arr = array();
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                $v['lvl'] = $lvl + 1;
                $v['leftpin'] = $leftpin + 0;
                $v['lefthtml'] = str_repeat($lefthtml, $lvl);
                $v['ltitle'] = $v['lefthtml'] . $v['title'];
                $arr[] = $v;
                $arr = array_merge($arr, self::menu($cate, $lefthtml, $v['id'], $lvl + 1, $leftpin + 20));
            }
        }

        return $arr;
    }

    /**
     * 权限设置选中状态
     * @param array $cate 栏目
     * @param int $pid 父ID
     * @param string $rules 规则
     * @return array
     */
    public static function authChecked($cate, $rules,$pid = 0)
    {
        $list = [];
        $rulesArr = explode(',', $rules);
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                $v['spread'] = true;
                if (self::authChecked($cate, $v['id'], $rules)) {
                    $v['children'] = self::authChecked($cate, $rules, $v['id']);
                } else {
                    if (in_array($v['id'], $rulesArr)) {
                        $v['checked'] = true;
                    }
                }
                $list[] = $v;
            }
        }
        return $list;
    }

    public static function arctypeToTree($list, $pid = 0)
    {
        $result = [];
        foreach ($list as $v) {
            if ($v['pid'] == $pid) {
                $v['spread'] = true;
                if (in_array($v["channel_id"],[4,6])) {
                    $v["disabled"] = true;
                }
                $v['children'] = self::arctypeToTree($list, $v['id']);
                $result[] = $v;
            }
        }
        return $result;
    }

    /**
     * 权限多维转化为二维
     * @param array $cate  栏目
     * @return array
     */
    public static function authNormal(array $cate): array
    {
        $list = [];
        foreach ($cate as $v) {
            $list[]['id'] = $v['id'];
//        $list[]['title'] = $v['title'];
//        $list[]['pid'] = $v['pid'];
            if (!empty($v['children'])) {
                $listChild = self::authNormal($v['children']);
                $list = array_merge($list, $listChild);
            }
        }
        return $list;
    }
}